﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace SPTotalAutomationToolkit.SP2013IntranetProjectTemplate.Wizard
{
    public partial class Page1 : UserControl
    {
        private WizardWindow mainWindow;

        internal Page1(WizardWindow mainWindow)
        {
            this.mainWindow = mainWindow;
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            // If the project template requires farm permissions disable the ability to select Sandbox
            if (!mainWindow.PresentationModel.IsSandboxed)
            {
                sandboxedSolutionRadioButton.IsEnabled = false;
            }

            // Default to a full trust farm solution
            farmSolutionRadioButton.IsChecked = true;
            mainWindow.PresentationModel.IsSandboxed = false;

            hostNamedScRadioButton.IsChecked = true;

            // Set default Customer and Project Names based on the solution name
            clientNameTextBox.Text = mainWindow.PresentationModel.ClientName;
            projectNameTextBox.Text = mainWindow.PresentationModel.ProjectName;
        }

        private void ClientNameTextBoxTextChanged(object sender, TextChangedEventArgs e)
        {
            mainWindow.PresentationModel.ClientName = clientNameTextBox.Text;
            ValidateForm();           
        }

        private void ProjectNameTextBoxTextChanged(object sender, TextChangedEventArgs e)
        {
            mainWindow.PresentationModel.ProjectName = projectNameTextBox.Text;
            ValidateForm();
        }

        private void sandboxedSolutionRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            mainWindow.PresentationModel.IsSandboxed = (bool)sandboxedSolutionRadioButton.IsChecked;
        }

        private void farmSolutionRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            mainWindow.PresentationModel.IsSandboxed = (bool)sandboxedSolutionRadioButton.IsChecked;
        }

        private void ValidateForm()
        {
            var formIsValid = true;

            if (clientNameTextBox.Text == "")
            {
                formIsValid = false;
            }

            if (projectNameTextBox.Text == "")
            {
                formIsValid = false;
            }
            
            if (formIsValid)
            {
                mainWindow.nextButton.IsEnabled = true;
            }
            else
            {
                mainWindow.nextButton.IsEnabled = false;
            }
        }

        private void hostNamedScRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            mainWindow.PresentationModel.UseHostNamedSiteCollections = (bool)hostNamedScRadioButton.IsChecked;
        }

        private void pathBasedScRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            mainWindow.PresentationModel.UseHostNamedSiteCollections = (bool)hostNamedScRadioButton.IsChecked;
        }
    }
}